package streaming;

import java.net.InetAddress;
import java.net.UnknownHostException;

import parameters.RLM_Parameters;
import streaming.control.AudioControl;
import streaming.control.VideoControl;
import utils.NICManager;

public class MediaCall implements Call{

	private InetAddress destination;
	private int port;
	boolean videoAvailable, videoRequested, videoEstablished;
	boolean audioAvailable, audioRequested, audioEstablished;
	
	private Conference videoConference;
	private Conference audioConference;
	
	public MediaCall() {
		// TODO Auto-generated constructor stub
		audioConference =  new Conference(Conference.MEDIA_AUDIO);
		videoConference = new Conference(Conference.MEDIA_VIDEO);
	}
	
	@Override
	public void hangUpCall() {
		// TODO Auto-generated method stub
		audioConference.hangUp();
		videoConference.hangUp();
	}

	/**
	 * Priprema MediaCall objekat za audio poziv. Na osnovu statusa, 
	 * kreira odgovarajuce komponente streaminga.
	 * 
	 * @param destination  	adresa odredista
	 * @param port  		port odredista
	 * @param status  		sadrzi informacije o inicijalizaciji 
	 * 						komponenata streaminga (slanje i prijem). 
	 * 						Dobija se {@link #getAudioStatus(boolean) getAudioStatus} metodom
	 * @param isMonitor		ako je true, omogucava korisniku da nadgleda stream koji salje.
	 * 			
	 */
	@Override
	public boolean initializeAudioCall(InetAddress destination, int port, int status, boolean isMonitor) {
		// TODO Auto-generated method stub
		
		return audioConference.initialize(destination, RLM_Parameters.getAudioPort(), port, status, false);
		
	}

	/**
	 * Priprema MediaCall objekat za video poziv. Na osnovu statusa, 
	 * kreira odgovarajuce komponente streaminga.
	 * 
	 * @param destination  	adresa odredista
	 * @param port  		port odredista
	 * @param status  		sadrzi informacije o inicijalizaciji 
	 * 						komponenata streaminga (slanje i prijem). 
	 * 						Dobija se {@link #getVideoStatus(boolean) getVideoStatus} metodom
	 * @param isMonitor		ako je true, omogucava korisniku da nadgleda stream koji salje.
	 * 			
	 */
	@Override
	public boolean initializeVideoCall(InetAddress destination, int port, int status, boolean isMonitor) {
		
		return videoConference.initialize(destination, RLM_Parameters.getVideoPort(), port, status, true);
	}

	@Override
	public boolean isAudioAvailable() {
		// TODO Auto-generated method stub
		return audioConference.isAvailable();
	}

	@Override
	public boolean isAudioEstablished() {
		// TODO Auto-generated method stub
		return audioEstablished;
	}

	@Override
	public boolean isAudioRequested() {
		// TODO Auto-generated method stub
		return audioRequested;
	}

	@Override
	public boolean isVideoAvailable() {
		// TODO Auto-generated method stub
		return videoConference.isAvailable();
	}

	@Override
	public boolean isVideoEstablished() {
		// TODO Auto-generated method stub
		return videoEstablished;
	}

	@Override
	public boolean isVideoRequested() {
		// TODO Auto-generated method stub
		return videoRequested;
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setAudioEstablished(boolean flag) {
		// TODO Auto-generated method stub
		this.audioEstablished = flag;
		this.audioRequested = false;
	}

	@Override
	public void setAudioRequested(boolean flag) {
		// TODO Auto-generated method stub
		this.audioRequested = flag;
	}

	@Override
	public void setVideoEstablished(boolean flag) {
		// TODO Auto-generated method stub
		this.videoEstablished = flag;
		this.videoRequested = false;
		this.audioRequested = false;
	}

	@Override
	public void setVideoRequested(boolean flag) {
		// TODO Auto-generated method stub
		this.videoRequested = flag;
	}

	@Override
	public void start() {
		audioConference.startCall();
		videoConference.startCall();
	}

	@Override
	public void stop() {
		// TODO Auto-generated method stub
		
	}
	/**
	 * Na osnovu dostupnosti audio streaminga kod ucesnika u konferenciji, 
	 * odredjuje koje komponente streaminga (slanje i primanje podataka)
	 * treba inicijalizovati. Povratna vrijednost ove metode se koristi u 
	 * pozivu {@link #initializeAudioCall(InetAddress, int, int, boolean) initializeAudioCall}
	 * 
	 * @param 	peerAvailable
	 * @return 	status ciji bitovi predstavljaju parametre za inicijalizaciju 
	 * 			dolaznih i odlaznih tokova audio poziva
	 *       	Moguce vrijednosti Conference.FROM_PEER, Conference.TO_PEER, Conference.BOTH
	 */
	public int getAudioStatus(boolean peerAvailable){
		return (peerAvailable?Conference.FROM_PEER:0) | (isAudioAvailable()?Conference.TO_PEER:0);
	}
	
	
	/**
	 * Na osnovu dostupnosti video streaminga kod ucesnika u konferenciji, 
	 * odredjuje koje komponente streaminga (slanje i primanje podataka)
	 * treba inicijalizovati. Povratna vrijednost ove metode se koristi u 
	 * pozivu {@link #initializeVideoCall(InetAddress, int, int, boolean) initializeVideoCall}
	 * 
	 * @param 	peerAvailable
	 * @return 	status ciji bitovi predstavljaju parametre za inicijalizaciju 
	 * 			dolaznih i odlaznih tokova video poziva.
	 *         	Moguce vrijednosti: Conference.FROM_PEER, Conference.TO_PEER, Conference.BOTH
	 */
	public int getVideoStatus(boolean peerAvailable){
		return (peerAvailable?Conference.FROM_PEER:0) | (isVideoAvailable()?Conference.TO_PEER:0);
	}
	
	public static void main(String[] args) {
		MediaCall call = new MediaCall();
		boolean aa = call.isAudioAvailable();
		boolean va = call.isVideoAvailable();
		
		RLM_Parameters.loadParameters();
		NICManager.readAdapters();
		
		int videoPort = 22222, audioPort = 22224;
		try {
			InetAddress add = InetAddress.getByName("");
			call.initializeAudioCall(add, audioPort, Conference.BOTH, false);
			call.initializeVideoCall(add, videoPort, Conference.BOTH, false);
			
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		
	}

	@Override
	public AudioControl getAudioControl() {
		// TODO Auto-generated method stub
		return (AudioControl)audioConference.getControl();
	}

	@Override
	public AudioControl getMonitorAudioControl() {
		// TODO Auto-generated method stub
		return (AudioControl)audioConference.getMonitorControl();
	}

	@Override
	public VideoControl getMonitorVideoControl() {
		// TODO Auto-generated method stub
		return (VideoControl)videoConference.getMonitorControl();
	}

	@Override
	public VideoControl getVideoControl() {
		// TODO Auto-generated method stub
		return (VideoControl)videoConference.getControl();
	}

	
	
}
